package com.my.adapter;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.my.bean.Note;
import com.my.helper.NoteDbOpenHelper;
import com.my.mynote.EditActivity;
import com.my.mynote.R;
import com.my.util.ToastUtil;

import java.util.List;

/**
 * @auther: 小鸥
 * @date:
 * @time: 15:43
 * @package: com.my.adapter
 * @project_name: My Note
 */
public class MyNoteAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    //数据实体
    private List<Note> mNoteList;
    private LayoutInflater mLayoutInflater;
    private Context mContext;
    private NoteDbOpenHelper mNoteDbOpenHelper;
    private int viewType;
    public static int TYPE_LINEAR_LAYOUT=0;
    public static int TYPE_GRID_LAYOUT=1;


    public MyNoteAdapter(List<Note> noteList, Context context) {
        //这里报过错mContext = context;要写在mLayoutInflater = LayoutInflater.from(mContext);的上面
        mNoteList = noteList;
        mContext = context;
        mLayoutInflater = LayoutInflater.from(mContext);
        mNoteDbOpenHelper = new NoteDbOpenHelper(mContext);
    }

    public void setViewType(int viewType) {
        this.viewType = viewType;
    }

    @Override
    public int getItemViewType(int position) {
        return viewType;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder  onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType==TYPE_LINEAR_LAYOUT){
            //创建ViewHolder---ViewHolder是一个条目----需要一个布局
            //解析my_note_adapter_list_item_layout.xml布局文件
            View view = mLayoutInflater.inflate(R.layout.my_note_adapter_list_item_layout,parent,false);
            MyViewHolder myViewHolder = new MyViewHolder(view);
            return myViewHolder;
        }else if(viewType==TYPE_GRID_LAYOUT){
            //创建ViewHolder---ViewHolder是一个条目----需要一个布局
            //解析my_note_adapter_list_item_layout.xml布局文件
            View view = mLayoutInflater.inflate(R.layout.my_note_adapter_grid_item_layout,parent,false);
            MyGridViewHolder myGridViewHolder = new MyGridViewHolder(view);
            return myGridViewHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        if (holder==null){
            return;
        }
        if (holder instanceof MyViewHolder){
            bindMyViewHolder((MyViewHolder) holder,position);
        }else if (holder instanceof MyGridViewHolder){
            bindMyGridViewHolder((MyGridViewHolder)holder,position);
        }
    }

    private void bindMyViewHolder(MyViewHolder holder,int position){
        //数据的绑定
        //拿到数据
        Note note = mNoteList.get(position);
        holder.mTitle.setText(note.getTitle());
        holder.mContent.setText(note.getContent());
        holder.mTime.setText(note.getCreate());
        //为外层的整个条目设置点击事件
        holder.mRlContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, EditActivity.class);
                intent.putExtra("note",note);
                mContext.startActivity(intent);
            }
        });
        //长按出弹窗
        holder.mRlContainer.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                //长按出弹窗，删除或者编辑
                //第二个参数：给对话框添加样式
                Dialog dialog = new Dialog(mContext, android.R.style.ThemeOverlay_Material_Dialog_Alert);

                View view = mLayoutInflater.inflate(R.layout.list_item_dialog, null);
                TextView delete = view.findViewById(R.id.tv_delete);
                TextView edit = view.findViewById(R.id.tv_edit);
                delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int i = mNoteDbOpenHelper.deleteFromDbById(note.getId());
                        //将数据删除
                        //通知适配器
                        if (i>0){
                            deleteData(position);
                            ToastUtil.showLongToast(mContext,"删除成功");
                        }else {
                            ToastUtil.showLongToast(mContext,"删除失败");
                        }
                        dialog.dismiss();
                    }
                });

                edit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext,EditActivity.class);
                        intent.putExtra("note",note);
                        mContext.startActivity(intent);
                        dialog.dismiss();
                    }
                });
                dialog.setContentView(view);
                dialog.show();
                return true;
            }
        });
    }
    private void bindMyGridViewHolder(MyGridViewHolder holder,int position){
        //数据的绑定
        //拿到数据
        Note note = mNoteList.get(position);
        holder.mTitle.setText(note.getTitle());
        holder.mContent.setText(note.getContent());
        holder.mTime.setText(note.getCreate());
        //为外层的整个条目设置点击事件
        holder.mRlContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, EditActivity.class);
                intent.putExtra("note",note);
                mContext.startActivity(intent);
            }
        });
        //长按出弹窗
        holder.mRlContainer.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                //长按出弹窗，删除或者编辑
                //第二个参数：给对话框添加样式
                Dialog dialog = new Dialog(mContext, android.R.style.ThemeOverlay_Material_Dialog_Alert);

                View view = mLayoutInflater.inflate(R.layout.list_item_dialog, null);
                TextView delete = view.findViewById(R.id.tv_delete);
                TextView edit = view.findViewById(R.id.tv_edit);
                delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int i = mNoteDbOpenHelper.deleteFromDbById(note.getId());
                        //将数据删除
                        //通知适配器
                        if (i>0){
                            deleteData(position);
                            ToastUtil.showLongToast(mContext,"删除成功");
                        }else {
                            ToastUtil.showLongToast(mContext,"删除失败");
                        }
                        dialog.dismiss();
                    }
                });

                edit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext,EditActivity.class);
                        intent.putExtra("note",note);
                        mContext.startActivity(intent);
                        dialog.dismiss();
                    }
                });
                dialog.setContentView(view);
                dialog.show();
                return true;
            }
        });
    }

    @Override
    public int getItemCount() {
        return mNoteList.size();
    }

    //刷新数据重新接收一个新的数据
    public void refreshData(List<Note> notes){
        this.mNoteList = notes;
        notifyDataSetChanged();
    }

    //长按删除
    public void deleteData(int pos){
        mNoteList.remove(pos);
        notifyItemRemoved(pos);
    }

    class MyViewHolder extends RecyclerView.ViewHolder{

        private final ViewGroup mRlContainer;
        private final TextView mTitle;
        private final TextView mTime;
        private final TextView mContent;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            //使用view来创建ViewHolder
            mRlContainer = itemView.findViewById(R.id.rl_list_item_container);
            mTitle = itemView.findViewById(R.id.title);
            mTime = itemView.findViewById(R.id.time);
            mContent = itemView.findViewById(R.id.content);
        }
    }

    class MyGridViewHolder extends RecyclerView.ViewHolder{

        private final ViewGroup mRlContainer;
        private final TextView mTitle;
        private final TextView mTime;
        private final TextView mContent;

        public MyGridViewHolder(@NonNull View itemView) {
            super(itemView);
            //使用view来创建ViewHolder
            mRlContainer = itemView.findViewById(R.id.rl_list_item_container);
            mTitle = itemView.findViewById(R.id.title);
            mTime = itemView.findViewById(R.id.time);
            mContent = itemView.findViewById(R.id.content);
        }
    }

}
